package net.listener;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zjitc.domain.Mail;
import net.service.MailService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.listener.SessionAwareMessageListener;
import org.springframework.stereotype.Component;

import javax.jms.*;

@Component
public class MailQueueMessageListener implements SessionAwareMessageListener {
   public static Logger logger = Logger.getLogger(MailQueueMessageListener.class);

    @Autowired
    private JmsTemplate jmsTemplate;
    @Autowired
    private Destination mailQueue;
    @Autowired
    private MailService mailService;
    @Override
    public synchronized void onMessage(Message message, Session session) throws JMSException {
        try {
            TextMessage textMessage = (TextMessage) message;
            final  String ms = textMessage.getText();
            logger.debug("receive by zhaojingjing "+ ms);
            Mail mail = JSONObject.parseObject(ms,Mail.class);

            if (mail!= null){
                try {
                    mailService.sendMail(mail);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
